package com.sync.syncmasterdata.customer.service.convert;

import com.sync.syncmasterdata.customer.entity.wms.WmsCustomer;
import com.sync.syncmasterdata.customer.pojo.CrmCustomer;
import com.sync.syncmasterdata.customer.service.impl.CPServiceImpl;
import com.sync.syncmasterdata.util.NormalUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * crm客户信息 转换 wms客户信息
 */
@Service
public class CrmToWms {

//    @Autowired
//    private WmsCustomerRepository repository;

    @Autowired
    private CPServiceImpl cpService;

    public WmsCustomer convertFun(CrmCustomer crm){
        WmsCustomer wms = new WmsCustomer();
        wms.setSCode(getNewId());
        wms.setUOrgId("C4ECB299-0803-4CBA-8895-51A2F1F5C661");
        wms.setSName(crm.getName());
        wms.setSShortName(crm.getName());
        wms.setSCustClass(getCusClass(crm.getNew_city()));
        wms.setSCurr("RMB");
        wms.setBUnActive(0);
        wms.setIOrder(1);
        wms.setSCurr("BS001");
        wms.setTCrtDate(NormalUtil.getNowTime());
        wms.setBFirstPass(0);
        wms.setSMemo("CRM推送");
        wms.setU8ID(crm.getNew_erp_num());
        return wms;
    }


    /**
     * 获取最后一个客户id
     * 婓创 GSP 停用
     * @return
     */
    @Deprecated
    private String getNewId() {
        String lastCode = null;
        try {
            String id = lastCode.substring(2, lastCode.length());
            return String.format("%s%d", "KH", Integer.valueOf(id) + 1);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 获取客户分类
     * @return
     */
    private String getCusClass(String _s){
        Map<String, JSONObject> map=this.cpService.getAreaMap(new JSONArray()).get("crm");
        if(map.containsKey(_s)){
            return map.get(_s).get("new_area").toString();
        }else{
            return null;
        }

    }
}
